package com.tjsinfo.tjsoa.addresslist.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddresslistSendBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending;
import com.tjsinfo.tjsoa.common.DESPlus;
import com.tjsinfo.tjsoa.message.TjsMessageIm;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.User;

public class SynchronizationAction extends Action{
	private IAddressListUserBo listUserBo;
	private IAddresslistSendBo sendBo;
	private ISystemUserBo tjsUserBo;
	private IAddressListAndFilePendingBo addressListAndFilePendingBo;
	
	public void setAddressListAndFilePendingBo(
			IAddressListAndFilePendingBo addressListAndFilePendingBo) {
		this.addressListAndFilePendingBo = addressListAndFilePendingBo;
	}
	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}
	public void setSendBo(IAddresslistSendBo sendBo) {
		this.sendBo = sendBo;
	}
	public void setListUserBo(IAddressListUserBo listUserBo) {
		this.listUserBo = listUserBo;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String yuanusers = request.getParameter("yuanusers");
		String sendusers = request.getParameter("sendusers");
		
		if ((yuanusers == null || "".equals(yuanusers)) && sendusers != null && !"".equals(sendusers)) {
			sendusers = sendusers.substring(0, sendusers.length()-1);
			String[] strs = sendusers.split(",");
			for (int i = 0; i < strs.length; i++) {
				if(
					addressListAndFilePendingBo.findBySendUserIdAndTjsUserAndTitle(user.getUser_id(),Integer.valueOf(strs[i]), "个人信息同步").size()>0){
					continue;
				}
				AddressListAndFilePending message = new AddressListAndFilePending();
				String uuid = UUID.randomUUID().toString().replace("-", "");
				message.setId(uuid);
				message.setSendUserId(user.getUser_id());
				message.setSendUserName(user.getUsername());
				message.setCreateTime(new Date());
				message.setTjsUserId(Integer.valueOf(strs[i]));
				message.setTitle("个人信息同步");
				message.setType("通讯录");
				String path = request.getContextPath();
				String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/tjsinfo/tjsoa/addresslist/toReturnSynchronization.do?str=";
				
				String str = ""+user.getUser_id()+"#"+strs[i]+"";
				try {
					DESPlus plus = new DESPlus();
					str = plus.encrypt(str);
				} catch (Exception e1) {
				}
				basePath += str;
				basePath += "&messageid="+uuid;
				message.setUrl(basePath);
				addressListAndFilePendingBo.save(message);

				TjsMessageIm messageIm = new TjsMessageIm();
				messageIm.sendMessage(strs[i], user.getUsername()+"向您发送通讯录个人信息同步请求");
			}
		}else if (sendusers == null || "".equals(sendusers)) {
			String[] str = yuanusers.substring(0,yuanusers.length()-1).split(",");
			String s = "";
			for (int i = 0; i < str.length; i++) {
				s += "'"+str[i]+"',";
			}
			
			listUserBo.updatesUserId(s.substring(0,s.length()-1), String.valueOf(user.getUser_id()));
		}else {
			yuanusers = ","+yuanusers;
			sendusers =	","+sendusers;
			String[] c = yuanusers.split(",");
			String deleteUsers = "";
			String e = ",";
			for (int i = 0; i < c.length; i++) {
				if (sendusers.indexOf(","+c[i]+",") == -1) {
					deleteUsers += c[i]+",";
				}else {
					e += c[i]+",";
				}
			}
			String[] f = sendusers.split(",");
			String insertUsers = "";
			for (int i = 0; i < f.length; i++) {
				if (e.indexOf(","+f[i]+",") == -1) {
					insertUsers += f[i]+",";
				}
			}
			insertUsers = insertUsers.substring(1, insertUsers.length());
			if (!",".equals(deleteUsers)) {
				deleteUsers = deleteUsers.replace(",", "','");
				deleteUsers = deleteUsers.substring(2,deleteUsers.length()-2);
				listUserBo.updatesUserId(deleteUsers, String.valueOf(user.getUser_id()));
			}
			if (insertUsers != null && !"".equals(insertUsers)) {
				String[] strs = insertUsers.split(",");
				for (int i = 0; i < strs.length; i++) {
					if(
						addressListAndFilePendingBo.findBySendUserIdAndTjsUserAndTitle(user.getUser_id(),Integer.valueOf(strs[i]), "个人信息同步").size()>0){
						continue;
					}
					
					AddressListAndFilePending message = new AddressListAndFilePending();
					String uuid = UUID.randomUUID().toString().replace("-", "");
					message.setId(uuid);
					message.setSendUserId(user.getUser_id());
					message.setSendUserName(user.getUsername());
					message.setCreateTime(new Date());
					message.setTjsUserId(Integer.valueOf(strs[i]));
					message.setTitle("个人信息同步");
					message.setType("通讯录");
					String path = request.getContextPath();
					String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/tjsinfo/tjsoa/addresslist/toReturnSynchronization.do?str=";
					
					String str = ""+user.getUser_id()+"#"+strs[i]+"";
					try {
						DESPlus plus = new DESPlus();
						str = plus.encrypt(str);
					} catch (Exception e1) {
					}
					basePath += str;
					basePath += "&messageid="+uuid;
					message.setUrl(basePath);
					addressListAndFilePendingBo.save(message);
					

					TjsMessageIm messageIm = new TjsMessageIm();
					messageIm.sendMessage(strs[i], user.getUsername()+"向您发送通讯录个人信息同步请求");
				}
			}
		
			request.setAttribute("state", "return");
			return new ActionForward("/toSynchronization.do?returnString=ok");
		}
		return new ActionForward("/toSynchronization.do?returnString=ok");
	}
}

